package com.project.collectAPI.entity.UploadImageAPI;

import com.alibaba.fastjson.JSONObject;
import com.project.collectAPI.comm.SpiderTools;
import com.project.collectAPI.entity.MeiTuan.SystemParam;
import lombok.Data;
import org.apache.http.client.config.RequestConfig;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Data
/*上传图片API*/
public class UploadImageAPI {
    /*
    * https://waimaiopen.meituan.com/api/v1/image/upload?app_id=124552&app_poi_code=18673732&img_name=jd.png&timestamp=170265255393227740d3b3ea1c31087f43282660e0c94
    * */

    private String reqAPI="https://waimaiopen.meituan.com/api/v1/image/upload";

    //APP方门店id，即商家中台系统里门店的编码。如商家在操作绑定门店至开放平台应用中时，未绑定三方门店id信息，则默认APP方门店id与美团门店id相同。
    private String app_poi_code;

    //图片字节流，图片转成 byte 数组的格式。注意：此字段不参与签名计算。
    private byte[] img_data;

    //图片名称，图片格式支持 [jpg][jpeg][png][bmp]
    private String img_name;

    //统一的时间戳
    private Long timestamp;

    //签名
    private String signValue;



    /*上传图片接口签名*/
    public String getSign() throws Exception {
        SystemParam systemParam=new SystemParam();
        Map<String,Object> map=new HashMap<>();
        //SystemParam.timestamp=System.currentTimeMillis();
        System.out.println("getSign() SystemParam.timestamp===>"+timestamp);
        map.put("timestamp",timestamp);  //timestamp尽量统一用一次
        map.put("app_id",systemParam.getAppId());
        map.put("app_poi_code",systemParam.getApp_poi_code());
        map.put("img_name",img_name);
        return systemParam.getSign(map,reqAPI); //获取签名
    }

    /*请求*/
    public String uploadImage() throws Exception {

        SystemParam systemParam=new SystemParam();
        //Post请求2
        Map<String, String> applicationParamsMap=new HashMap<>();
        applicationParamsMap.put("app_id",systemParam.getAppId());
        applicationParamsMap.put("timestamp",String.valueOf(timestamp));
        applicationParamsMap.put("sig",signValue);
        applicationParamsMap.put("app_poi_code",systemParam.getApp_poi_code());
        applicationParamsMap.put("img_name",img_name);
        RequestConfig.Builder requestConfigBuilder = RequestConfig.custom();
        // ConnectTimeout:连接超时.连接建立时间，三次握手完成时间.
        requestConfigBuilder.setConnectTimeout( 30000* 1000);
        String res= SpiderTools.requestOfPost(reqAPI, applicationParamsMap,img_data,img_name,requestConfigBuilder);
        System.out.println("图片接口上传结果================>");
        System.out.println(res);

        JSONObject rct=JSONObject.parseObject(res);
        String data= (!Objects.isNull(rct.get("data")))?rct.getString("data"):null;

        //System.out.println("返回图片id:===>"+data);
        //{"data":"D2AFA406ABDEC1843C78C4BBDA46980B","result_code":1,"success_map":{"data":"D2AFA406ABDEC1843C78C4BBDA46980B"}}
        return data;

        /*Map<String,Object> map=new HashMap<>();
        //系统级参数
        map.put("timestamp",timestamp);
        map.put("app_id",systemParam.getAppId());
        map.put("sig",signValue);
        //应用级参数
        map.put("app_poi_code",systemParam.getApp_poi_code());
        map.put("img_data",img_data);
        map.put("img_name",img_name);
        //将map转换为jsonObject对象
        JSONObject jsonObject= new JSONObject(map);
        String res= SpiderTools.HttpPostJson(reqAPI,jsonObject);
        System.out.println("图片上传结果:"+res);*/


        /*List<NameValuePair> nameValues=new ArrayList<NameValuePair>();
        nameValues.add(new BasicNameValuePair("timestamp", String.valueOf(timestamp)));
        nameValues.add(new BasicNameValuePair("app_id", systemParam.getAppId()));
        nameValues.add(new BasicNameValuePair("sig",signValue));
        nameValues.add(new BasicNameValuePair("app_poi_code",systemParam.getApp_poi_code()));
        //将byte[] 转换为字符串
        String base64EncodedData = Base64Utils.encodeToString(img_data);
        nameValues.add(new BasicNameValuePair("img_data",base64EncodedData));
        nameValues.add(new BasicNameValuePair("img_name", img_name));
        String reqApiUrl=reqAPI+"?app_id="+systemParam.getAppId()+"&timestamp="+timestamp+"&sig="+signValue;
        System.out.println("传递参数:"+nameValues);
        System.out.println("请求地址:"+reqApiUrl);
        String res= SpiderTools.HttpPost(reqApiUrl, nameValues);*/


        /*MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        System.out.println("uploadImage() SystemParam.timestamp===>"+timestamp);
        body.add("app_id", systemParam.getAppId());
        body.add("timestamp",timestamp); //timestamp尽量统一用一次
        body.add("app_poi_code", systemParam.getApp_poi_code());
        ByteArrayResource resource = new ByteArrayResource(img_data);
        body.add("img_data", img_data);
        body.add("img_name", img_name);
        body.add("sig", signValue);
        System.out.println("输入参数:"+body);
        String reqApiUrl=reqAPI+"?app_id="+systemParam.getAppId()+"&timestamp="+timestamp+"&sig="+signValue;
        System.out.println("发送请求API:"+reqApiUrl);
        String res= SpiderTools.HttpPost_MultipartFormData(reqAPI, body);*/



        //Post请求
        /*Map<String, String> applicationParamsMap=new HashMap<>();
        applicationParamsMap.put("app_id",systemParam.getAppId());
        applicationParamsMap.put("timestamp",String.valueOf(timestamp));
        applicationParamsMap.put("sig",signValue);
        applicationParamsMap.put("app_poi_code",systemParam.getApp_poi_code());
        //ByteArrayResource resource = new ByteArrayResource(img_data);
        //applicationParamsMap.put("img_data",new String((byte[])img_data,StandardCharsets.UTF_8));
        //applicationParamsMap.put("img_data",img_data);
        applicationParamsMap.put("img_name",img_name);
        //System.out.println("输入参数:"+applicationParamsMap);

        *//*Map<String, byte[]> bytemap=new HashMap<>();
        bytemap.put("img_data",img_data);
        applicationParamsMap.putAll(bytemap);

        System.out.println("合并:"+applicationParamsMap);*//*

        String res= SpiderTools.requestOfPostAsync(reqAPI, applicationParamsMap);
        System.out.println("图片接口上传结果================>");
        System.out.println(res);*/



    }

}
